package com.yenlien.traceyun.usercenter.service;

import com.yenlien.traceyun.usercenter.dao.OtherMaterialDao;
import com.yenlien.traceyun.usercenter.entity.OtherMaterial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by LiuSha on 2020/6/18.
 */
@Service
public class OtherMaterialService {
    @Autowired
    private OtherMaterialDao otherMaterialDao;
    @Autowired
    private FileService fileService;

    @Transactional
    public void save(List<OtherMaterial> materialList, String relevanceId, List<MultipartFile> files) throws Exception {
        List<OtherMaterial> otherMaterials = findByRelevanceId(relevanceId);
        List<String> deleteIds = new ArrayList<>();
        if (otherMaterials.size() > 0) {
            deleteIds = otherMaterials.stream().map(p -> p.getId()).collect(Collectors.toList());
        }
        for (OtherMaterial otherMaterial : materialList) {
            otherMaterial.setRelevanceId(relevanceId);
            if (StringUtils.isEmpty(otherMaterial.getId())) {
                fileService.upload(null, otherMaterial, files);
            } else {
                fileService.upload(otherMaterialDao.findById(otherMaterial.getId()), otherMaterial, files);
            }
            otherMaterialDao.save(otherMaterial);
            if (deleteIds.size() > 0 && deleteIds.contains(otherMaterial.getId())) {
                deleteIds.remove(otherMaterial.getId());
            }
        }
        deletes(deleteIds);
    }

    @Transactional
    public void save2(List<OtherMaterial> materialList, String relevanceId) throws Exception {
        List<OtherMaterial> otherMaterials = findByRelevanceId(relevanceId);
        List<String> deleteIds = new ArrayList<>();
        if (otherMaterials.size() > 0) {
            deleteIds = otherMaterials.stream().map(p -> p.getId()).collect(Collectors.toList());
        }
        for (OtherMaterial otherMaterial : materialList) {
            otherMaterial.setRelevanceId(relevanceId);
            otherMaterialDao.save(otherMaterial);
            if (deleteIds.size() > 0 && deleteIds.contains(otherMaterial.getId())) {
                deleteIds.remove(otherMaterial.getId());
            }
        }
        deletes(deleteIds);
    }



    public List<OtherMaterial> findByRelevanceId(String id) {
        return otherMaterialDao.findByRelevanceId(id);
    }

    public void deletes(List<String> ids) throws IOException {
        for (String id : ids) {
            OtherMaterial otherMaterial = otherMaterialDao.findById(id);
            if (!StringUtils.isEmpty(otherMaterial.getMaterialImg())) {
                fileService.deleteFile(otherMaterial.getMaterialImg());
            }
            otherMaterialDao.delete(otherMaterial);
        }
    }


}
